import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import { getToken } from '@/utils/auth' // get token from cookie

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard', affix: true }
    }]
  },

  {
    path: '/sys',
    component: Layout,
    redirect: '/sys/user',
    name: 'sysManage',
    meta: { title: '用户管理', icon: 'sysmanage'},
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/sys/user'),
        meta: { title: '用户管理', icon: 'usermanage'},
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/sys/role'),
        meta: { title: '角色管理', icon: 'rolemanage'}
      },
      {
        path: 'dep',
        name: 'dep',
        component: () => import('@/views/sys/dep'),
        meta: { title: '机房管理', icon: 'el-icon-office-building'}
      },
    ]
  },

  {
    path: '/test',
    component: Layout,
    redirect: '/test/test',
    name: 'testManage',
    meta: { title: '考试相关', icon: 'form'},
    children: [
      {
        path: 'test',
        name: 'test',
        component: () => import('@/views/test/test'),
        meta: { title: '日常训练', icon: 'table'}
      },
      {
        path: 'flyExam',
        name: 'flyExam',
        component: () => import('@/views/test/flyExam'),
        meta: { title: '飞检考试', icon: 'el-icon-notebook-2'}
      },
      {
        path: 'history',
        name: 'history',
        component: () => import('@/views/test/history'),
        meta: { title: '历史考试记录', icon: 'el-icon-receiving'}
      },
    ]
  },

  {
    path: '/testMange',
    component: Layout,
    redirect: '/testMange/course',
    name: 'testManage1',
    meta: { title: '考试管理', icon: 'el-icon-box'},
    hidden: false,
    children: [
      {
        path: 'course',
        name: 'course',
        component: () => import('@/views/testMange/course'),
        meta: { title: '科目', icon: 'el-icon-reading'}
      },
      {
        path: 'paper',
        name: 'paper',
        component: () => import('@/views/testMange/paper'),
        meta: { title: '试卷管理', icon: 'el-icon-notebook-1'}
      },
      {
        path: 'flyPaperDesc',
        name: 'flyPaperDesc',
        component: () => import('@/views/testMange/flyPaperDesc'),
        meta: { title: '试卷详情', icon: 'el-icon-document'}
      },
      {
        path: 'questionDesc',
        name: 'questionDesc',
        component: () => import('@/views/testMange/questionDesc'),
        meta: { title: '题库', icon: 'el-icon-takeaway-box'}
      },
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = []

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// router.beforeEach(async (to, from, next) => {
//   // console.log('每次导航之前');
//   const hasToken = getToken()
//   // console.log(hasToken)
//   if (hasToken) {
//     // console.log('存在令牌。正在获取用户信息...');
//     await store.dispatch('user/getInfo') // 等待异步操作完成
//     .then(() => {
//       // console.log('成功获取用户信息');
//     })
//     .catch(error => {
//       console.error('获取用户信息时出错：', error);
//     });
//   } else {
//     // console.log('未找到令牌');
//     next();
//   }
//   // console.log('检查用户角色...');
//   const isSuper = store.getters.isSuper
//   // console.log(isSuper)
//   // 根据用户权限动态设置路由的 hidden 属性
//   constantRoutes.forEach(route => {
//     if (route.meta && route.hidden === false) {
//       route.hidden = isSuper !== 1; // 设置为 true 表示隐藏，根据实际情况调整
//     }
//     // route.alwaysShow = true;
//   })
//   next();
// })

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
